package com.open.basic;

import com.open.basic.constant.NumberConstants;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import java.io.Serial;
import java.io.Serializable;

/** 分页请求对象
 * @author: system
 * @Date: 2024/4/11 23:42
 * @Version: V1.0
 * @description: 分页对象
 */
@Getter
@ToString
public class Page<T> implements Serializable {

    @Serial
    private static final long serialVersionUID = 1L;

    /**
     * 最大每页数目，1000，避免OOM
     */
    private static final int MAX_PAGE_SIZE = NumberConstants.NUMBER_1000;

    private static final int PAGE_NO_DEFAULT = NumberConstants.NUMBER_1;

    private static final int PAGE_SIZE_DEFAULT = NumberConstants.NUMBER_10;


    /**
     * 请求查询的页码
     */
    @Setter
    @NotNull(message = "pageNo不能为空")
    @Max(value = Integer.MAX_VALUE, message = "pageNo超过最大值")
    @Min(value = 1, message = "pageNo必须大于等于1")
    private Integer pageNo;

    /**
     * 每页显示条数
     */
    @Setter
    @NotNull(message = "pageSize不能为空")
    @Max(value = MAX_PAGE_SIZE, message = "pageSize过大")
    @Min(value = 1, message = "pageSize必须大于等于1")
    private Integer pageSize;

    /**
     * 升序字段 逗号分割
     */
    private String asc;

    /**
     * 降序字段 逗号分割
     */
    private String desc;

    public Page() {
        this.pageNo = PAGE_NO_DEFAULT;
        this.pageSize = PAGE_SIZE_DEFAULT;
    }

    public Page(Integer pageNo, Integer pageSize) {
        this.pageNo = pageNo;
        this.pageSize = pageSize;
    }

    public Page<T> setAsc(String asc) {
        this.asc = asc;
        return this;
    }

    public Page<T> setDesc(String desc) {
        this.desc = desc;
        return this;
    }

    // public com.baomidou.mybatisplus.extension.plugins.pagination.Page<T> toMybatisPlusPage() {
    //     FunctionUtil.PAGE_CHECK.accept(this);
    //     return new com.baomidou.mybatisplus.extension.plugins.pagination.Page<T>(pageNo, pageSize);
    // }
}
